/*
 * ========================================================
 * Copyright(c) 2014 杭州偶尔科技-版权所有
 * ========================================================
 * 本软件由杭州偶尔技所有, 未经书面许可, 任何单位和个人不得以
 * 任何形式复制代码的部分或全部, 并以任何形式传播。
 * 公司网址
 *
 * 			http://www.ouer.com/
 *
 * ========================================================
 */

package com.simon.mengkou.ui.dialog;


import android.app.Activity;
import android.content.DialogInterface;
import android.os.Bundle;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.ouertech.android.agm.lib.base.constant.CstFile;
import com.ouertech.android.agm.lib.base.future.core.AgnettyFuture;
import com.ouertech.android.agm.lib.base.future.core.AgnettyResult;
import com.ouertech.android.agm.lib.base.utils.UtilFile;
import com.ouertech.android.agm.lib.base.utils.UtilMD5;
import com.ouertech.android.agm.lib.base.utils.UtilPackage;
import com.ouertech.android.agm.lib.base.utils.UtilStorage;
import com.ouertech.android.agm.lib.ui.base.BaseDialog;
import com.simon.mengkou.R;
import com.simon.mengkou.data.bean.base.UpdateInfo;
import com.simon.mengkou.future.base.OuerFutureListener;
import com.simon.mengkou.system.global.OuerApplication;
import com.simon.mengkou.utils.UtilOuer;


/**
 * @author : Zhenshui.Xia
 * @date   : 2014-8-11
 * @desc   : 应用下载对话框
 */
public class AppDownloadDialog extends BaseDialog {
	private Activity mActivity;
    private UpdateInfo mInfo;
    //进度条
    private ProgressBar mPbProgress;
    //进度值
    private TextView mTvProgress;

    public AppDownloadDialog(Activity activity, UpdateInfo info) {
        super(activity);
        this.mActivity = activity;
        this.mInfo = info;
    }

    public AppDownloadDialog(Activity activity, UpdateInfo info, int theme){
        super(activity, theme);
        this.mActivity = activity;
        this.mInfo = info;
    }

    @Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		setTitle(R.string.upgrade_download_title);
		setSingle(R.string.upgrade_cancel);

		//取消下载
		setOnSingleListener(new OnSingleListener() {

			@Override
			public void onSingle() {
				cancel();
			}
		});

	}

	@Override
	protected void initLayout() {
		setContentView(R.layout.dialog_app_download);
	}

	@Override
	protected void initViews() {
		mTvProgress = (TextView)findViewById(R.id.app_download_id_progress);
		mPbProgress = (ProgressBar)findViewById(R.id.app_download_id_progressbar);
	}

  @Override
	protected void onStart() {
		super.onStart();

		String fileName = UtilMD5.getStringMD5(mInfo.getUrl()) + CstFile.SUFFIX_APK;
		final String filePath = UtilStorage.createFilePath(mActivity, fileName);

		final AgnettyFuture future = OuerApplication.mOuerFuture.execDownloadFuture(mInfo.getUrl(), filePath,
                new OuerFutureListener(mActivity){

				@Override
				public void onStart(AgnettyResult result) {
					super.onStart(result);
					//开始
					mPbProgress.setProgress(0);
					mTvProgress.setText(mActivity.getString(R.string.upgrade_download_progress, 0));
				}

				@Override
				public void onProgress(AgnettyResult result) {
					super.onProgress(result);
					//更新进度
					int progress = result.getProgress();
					mPbProgress.setProgress(progress);
					mTvProgress.setText(mActivity.getString(R.string.upgrade_download_progress, progress));
				}

				@Override
				public void onComplete(AgnettyResult result) {
					super.onComplete(result);
					//下载完成，校验安装包，成功安装应用，否则删除该安装包
					if(UtilMD5.getFileMD5(filePath).equals(mInfo.getMd5())) {
						UtilPackage.installApk(mContext, filePath);
					} else {
						UtilFile.deleteFile(filePath);
					}
					cancel();
				}

				@Override
				public void onNetUnavaiable(AgnettyResult result) {
					super.onNetUnavaiable(result);
					//网络不给力
					cancel();
				}

				@Override
				public void onException(AgnettyResult result) {
					super.onException(result);
					//下载过程发生异常
					UtilOuer.toast(mActivity, R.string.upgrade_download_failure);
					cancel();
				}

			});

		/**
		 * 监听对话框退出事件，取消下载任务，强制更新则退出应用
		 */
		setOnCancelListener(new OnCancelListener() {

			@Override
			public void onCancel(DialogInterface arg0) {
				future.cancel();

				if(mInfo.isForceUpdate()) {
					mActivity.finish();
				}
			}
		});
  	}
}


